"""
定義接口測試用例
使用 unittest
1.導包
2.創建測試類
    前置方法
    後置方法
    測試方法
"""

# 導包
import requests
import unittest

import app
from api.login import LoginAPI
# from parameterized import parameterized

# 創建測試類
from utils import common_assert


class TestLogin(unittest.TestCase):
    # 前置方法
    def setUp(self):
        # 實例化接口類對象 login_api
        self.login_api = LoginAPI()

    # 後置方法(無)

    # 測試方法
    # 定义测试用例
    # case001 登录成功
    def test01_case001(self):
        # 调用登录接口进行登录
        response = self.login_api.login({"mobile": "13800000002", "password":
            "123456"})
        print(response.json())
        # 断言
        # self.assertEqual(200, response.status_code)
        # self.assertEqual(True, response.json().get("success"))
        # self.assertEqual(10000, response.json().get("code"))
        # self.assertIn("操作成功", response.json().get("message"))
        # common_assert(self, response, 200, True, 10000, "操作成功")
        common_assert(self, response)   # 直接使用公共断言方法的默认值即可

        # 提取token數據
        app.TOKEN = "Bearer " + response.json().get("data")
        # 附值給headers_data中的Authorization
        app.headers_data["Authorization"] = app.TOKEN

    # case002 不输入手机号
    def test02_case002(self):
        # 调用登录接口进行登录
        response = self.login_api.login({"mobile": "", "password": "123456"})
        print(response.json())
        # 断言
        # self.assertEqual(200, response.status_code)
        # self.assertEqual(False, response.json().get("success"))
        # self.assertEqual(20001, response.json().get("code"))
        # self.assertIn("用户名或密码错误", response.json().get("message"))
        common_assert(self, response, 200, False, 20001, "用户名或密码错误")

    # case003 不输入密码
    def test03_case003(self):
        # 调用登录接口进行登录
        response = self.login_api.login({"mobile": "13800000002", "password":
            ""})
        print(response.json())
        # 断言
        # self.assertEqual(200, response.status_code)
        # self.assertEqual(False, response.json().get("success"))
        # self.assertEqual(20001, response.json().get("code"))
        # self.assertIn("用户名或密码错误", response.json().get("message"))
        common_assert(self, response, 200, False, 20001, "用户名或密码错误")

    # case004 手机长度小于11位
    def test04_case004(self):
        # 调用登录接口进行登录
        response = self.login_api.login({"mobile": "1380000000", "password":
            "123456"})
        print(response.json())
        # 断言
        # self.assertEqual(200, response.status_code)
        # self.assertEqual(False, response.json().get("success"))
        # self.assertEqual(20001, response.json().get("code"))
        # self.assertIn("用户名或密码错误", response.json().get("message"))
        common_assert(self, response, 200, False, 20001, "用户名或密码错误")

    # case005 手机场地大于11位
    def test05_case005(self):
        # 调用登录接口进行登录
        response = self.login_api.login({"mobile": "138000000023", "password":
            "123456"})
        print(response.json())
        # 断言
        # self.assertEqual(200, response.status_code)
        # self.assertEqual(False, response.json().get("success"))
        # self.assertEqual(20001, response.json().get("code"))
        # self.assertIn("用户名或密码错误", response.json().get("message"))
        common_assert(self, response, 200, False, 20001, "用户名或密码错误")

    # case006 手机号输入非数字
    def test06_case006(self):
        # 调用登录接口进行登录
        response = self.login_api.login({"mobile": "error", "password":
            "123456"})
        print(response.json())
        # 断言
        # self.assertEqual(200, response.status_code)
        # self.assertEqual(False, response.json().get("success"))
        # self.assertEqual(20001, response.json().get("code"))
        # self.assertIn("用户名或密码错误", response.json().get("message"))
        common_assert(self, response, 200, False, 20001, "用户名或密码错误")

    # case007 输入未注册手机号
    def test07_case007(self):
        # 调用登录接口进行登录
        response = self.login_api.login({"mobile": "13812321236", "password":
            "123456"})
        print(response.json())
        # 断言
        # self.assertEqual(200, response.status_code)
        # self.assertEqual(False, response.json().get("success"))
        # self.assertEqual(20001, response.json().get("code"))
        # self.assertIn("用户名或密码错误", response.json().get("message"))
        common_assert(self, response, 200, False, 20001, "用户名或密码错误")

    # case008 多参
    def test08_case008(self):
        # 调用登录接口进行登录
        response = self.login_api.login({"mobile": "13800000002", "password":
            "123456", "haha": "xixi"})
        print(response.json())
        # self.assertEqual(200, response.status_code)
        # self.assertEqual(True, response.json().get("success"))
        # self.assertEqual(10000, response.json().get("code"))
        # self.assertIn("操作成功", response.json().get("message"))
        common_assert(self, response, 200, True, 10000, "操作成功")

    # case009 少参-缺少手机号
    def test09_case009(self):
        # 调用登录接口进行登录
        response = self.login_api.login({"password": "123456"})
        print(response.json())
        # 断言
        # self.assertEqual(200, response.status_code)
        # self.assertEqual(False, response.json().get("success"))
        # self.assertEqual(20001, response.json().get("code"))
        # self.assertIn("用户名或密码错误", response.json().get("message"))
        common_assert(self, response, 200, False, 20001, "用户名或密码错误")

    # case010 少参-缺少密码
    def test10_case010(self):
        # 调用登录接口进行登录
        response = self.login_api.login({"mobile": "13800000002"})
        print(response.json())
        # 断言
        # self.assertEqual(200, response.status_code)
        # self.assertEqual(False, response.json().get("success"))
        # self.assertEqual(20001, response.json().get("code"))
        # self.assertIn("用户名或密码错误", response.json().get("message"))
        common_assert(self, response, 200, False, 20001, "用户名或密码错误")

    # case011 无参
    def test11_case011(self):
        # 调用登录接口进行登录
        response = self.login_api.login(None)
        print(response.json())
        # 断言
        self.assertEqual(200, response.status_code)
        self.assertEqual(False, response.json().get("success"))
        self.assertEqual(99999, response.json().get("code"))
        self.assertIn("抱歉，系统繁忙", response.json().get("message"))
        common_assert(self, response, 200, False, 99999, "抱歉，系统繁忙")

    # case012 错误参数-手机号
    def test12_case012(self):
        # 调用登录接口进行登录
        response = self.login_api.login({"mobiel": "13800000002", "password":
            "123456"})
        print(response.json())
        # 断言
        # self.assertEqual(200, response.status_code)
        # self.assertEqual(False, response.json().get("success"))
        # self.assertEqual(20001, response.json().get("code"))
        # self.assertIn("用户名或密码错误", response.json().get("message"))
        common_assert(self, response, 200, False, 20001, "用户名或密码错误")

    # case013 错误参数-密码
    def test13_case013(self):
        # 调用登录接口进行登录
        response = self.login_api.login({"mobile": "13800000002", "passwd":
            "123456"})
        print(response.json())
        # 断言
        # self.assertEqual(200, response.status_code)
        # self.assertEqual(False, response.json().get("success"))
        # self.assertEqual(20001, response.json().get("code"))
        # self.assertIn("用户名或密码错误", response.json().get("message"))
        common_assert(self, response, 200, False, 20001, "用户名或密码错误")